﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QualityRpt.Dao;
using QualityRpt.Utils;
using System.Threading.Tasks;

namespace QualityRpt.Service
{
    /// <summary>
    /// 低电数据服务类
    /// </summary>
    public class TransLBService:BaseService
    {
        private static ITransLBDao _impDao = null;
        static TransLBService()
        {
            _impDao = CreateDaoInstance("ITransLBDao") as ITransLBDao;
        }
        /// <summary>
        /// 低电标签综合查询支持
        /// </summary>
        public static DataTable GetOBUStatByQ(DateTime startTime, DateTime endTime, int statTType, string spin, string specin, string cnin,
            string rdin, string plazain, string lanein, bool fromTemp, string[] numstartArray, string[] numendArray)
        {
            return _impDao.GetOBUStatByQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray);
        }
        public static DataTable GetDetailByQ(DateTime startTime, DateTime endTime, string spin, string specin, string cnin, string rdin,
            string plazain, string lanein, bool fromTempTable, string[] numstartArray, string[] numendArray, PageUtils pu)
        {
            return _impDao.GetDetailByQ(startTime, endTime,spin,specin,cnin,rdin,plazain,lanein,fromTempTable,numstartArray,numendArray,pu );
        }
        /// <summary>
        /// 以标签号分组的明细记录v1.2
        /// </summary>
        public static DataTable GetOBUGDetailByQ(DateTime startTime, DateTime endTime, string spin, string specin, string cnin, string rdin,
            string plazain, string lanein, bool fromTempTable, bool hasUnread, string[] numstartArray, string[] numendArray, PageUtils pu)
        {
            DataTable tampobuTable = _impDao.GetOBUGDetailByQ(startTime, endTime, spin, specin, cnin, rdin, plazain, lanein, fromTempTable, numstartArray, numendArray, pu);
            DataView dv = new DataView(tampobuTable);
            DataTable obunotable = dv.ToTable(true, "OBUNO");
            StringBuilder sbobuno = new StringBuilder();
            for (int i = 0; i < obunotable.Rows.Count; i++)
            {
                if (i > 0)
                {
                    sbobuno.Append(",");
                }
                sbobuno.Append(obunotable.Rows[i]["OBUNO"].ToString());
            }
            DataTable obuTable = TransNoteService.GetOBUGDetailByQ(startTime, endTime, sbobuno.ToString(), rdin, plazain, lanein);
            List<string> columnNames = new List<string>();
            for (int i = 0; i < tampobuTable.Columns.Count - 1; i++)
            {
                columnNames.Add(tampobuTable.Columns[i].ColumnName);
            }
            DataTable resultTable = dv.ToTable(true, columnNames.ToArray());
            resultTable.Columns.Add("LB_TRANSCOUNT", typeof(int));
            resultTable.Columns.Add("TRANSCOUNT", typeof(int));
            resultTable.Columns.Add("LB_RATE", typeof(decimal), "iif(TRANSCOUNT>0,LB_TRANSCOUNT/TRANSCOUNT,null)");
            StringBuilder selText = new StringBuilder();
            for (int i = 0; i < resultTable.Rows.Count; i++)
            {
                selText.Clear(); DataRow curRow = resultTable.Rows[i];
                for (int j = 0; j < columnNames.Count; j++)
                {
                    string clName = columnNames[j];
                    DataColumn _dtc = resultTable.Columns[clName];
                    if (j > 0)
                    {
                        selText.Append(" AND ");
                    }
                    if (_dtc.DataType.Equals(typeof(string)))
                    {
                        selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                    }
                    else
                    {
                        selText.Append(clName + "=" + curRow[clName].ToString());
                    }
                }
                DataRow[] lbSelRows = tampobuTable.Select(selText.ToString());
                DataRow[] SelRows = obuTable.Select(selText.ToString());
                if (lbSelRows.Length > 0)
                {
                    curRow["LB_TRANSCOUNT"] = lbSelRows[0]["TRANSCOUNT"];
                }
                if (SelRows.Length > 0)
                {
                    curRow["TRANSCOUNT"] = SelRows[0]["TRANSCOUNT"];
                }
            }
            return resultTable;
        }
    }
}
